package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.model.Model;

import java.util.ArrayList;
import java.util.List;

public class RankActUserData extends RankActBaseData {

    public static UserActTopRank getUserActTopRank(long userId, long actId) {
        Model model = EntityManager.getEntity(UserActTopRank.class, userId, actId);
        if (model != null) {
            return (UserActTopRank) model;
        }
        return null;
    }
    public static UnionActTopRank getUnionActTopRank(long unionId, long actId) {
        Model model = EntityManager.getEntity(UnionActTopRank.class, unionId, actId);
        if (model != null) {
            return (UnionActTopRank) model;
        }
        return null;
    }

    public static RankActGroup getRankActGroup(int groupId, long round) {
        Model model = EntityManager.getEntity(RankActGroup.class, groupId, round);
        if (model != null) {
            return (RankActGroup) model;
        }
        return null;
    }

    public static UserActTopCrossRank getUserActTopCrossRank(long userId, long actId, long actTime) {
        Model model = EntityManager.getEntity(UserActTopCrossRank.class, userId, actId);
        if (model != null) {
            UserActTopCrossRank userActTopCrossRank = (UserActTopCrossRank) model;
//            if (userActTopCrossRank.getActTime() != actTime) {
//                userActTopCrossRank.putScore(0);
//                userActTopCrossRank.putMaxScore(0);
//                userActTopCrossRank.putActTime(actTime);
//            }
            return userActTopCrossRank;
        }
        return null;
    }
    public static UnionActTopCrossRank getUnionActTopCrossRank(long unionId, long actId, long actTime) {
        Model model = EntityManager.getEntity(UnionActTopCrossRank.class, unionId, actId);
        UnionActTopCrossRank unionActTopCrossRank = null;
        if (model != null) {
            unionActTopCrossRank = (UnionActTopCrossRank) model;
//            if (unionActTopCrossRank.getActTime() != actTime) {
//                unionActTopCrossRank.putScore(0);
//                unionActTopCrossRank.putMaxScore(0);
//                unionActTopCrossRank.putActTime(actTime);
//            }
            return unionActTopCrossRank;
        }
        return null;
    }

    public static UserRankActTask getUserRankActTask(long userId, long round, int actId, int taskType) {
        String listKey = EntityManager.getFieldJoinString(userId, actId);
        Model model = EntityManager.getEntity(UserRankActTask.class, listKey, taskType);
        UserRankActTask userRankActTask = null;
        if (model != null) {
            userRankActTask = (UserRankActTask) model;
            if (userRankActTask.getRound() != round) {
                userRankActTask.checkReset(round);
            }
        }
        return userRankActTask;
    }

    public static List<UserRankActTask> getUserRankActTaskList(long userId, long round, int actId) {
        String listKey = EntityManager.getFieldJoinString(userId, actId);
        List<Model> list = EntityManager.getEntityList(UserRankActTask.class, listKey);
        List<UserRankActTask> result = new ArrayList<>();
        for (Model model : list) {
            UserRankActTask userRankActTask = (UserRankActTask) model;
            if (userRankActTask.getRound() != round) {
                userRankActTask.checkReset(round);
            }
            result.add(userRankActTask);
        }
        return result;
    }

    public static List<UserRankActTask> getRankActTaskList(long userId, int actId) {
        String listKey = EntityManager.getFieldJoinString(userId, actId);
        List<Model> list = EntityManager.getEntityList(UserRankActTask.class, listKey);
        List<UserRankActTask> result = new ArrayList<>();
        for (Model model : list) {
            result.add((UserRankActTask) model);
        }
        return result;
    }

    public static UserCrossRankActTask getUserCrossRankActTask(long userId, long round, int actId, int taskType) {
        String listKey = EntityManager.getFieldJoinString(userId, actId);
        Model model = EntityManager.getEntity(UserCrossRankActTask.class, listKey, taskType);
        UserCrossRankActTask userCrossRankActTask = null;
        if (model != null) {
            userCrossRankActTask = (UserCrossRankActTask) model;
            if (userCrossRankActTask.getRound() != round) {
                userCrossRankActTask.checkReset(round);
            }
        }
        return userCrossRankActTask;
    }

    public static List<UserCrossRankActTask> getUserCrossRankActTaskList(long userId, int actId) {
        String listKey = EntityManager.getFieldJoinString(userId, actId);
        List<Model> list = EntityManager.getEntityList(UserCrossRankActTask.class, listKey);
        List<UserCrossRankActTask> result = new ArrayList<>();
        for (Model model : list) {
            result.add((UserCrossRankActTask) model);
        }
        return result;
    }

    public static List<UserRankAct> getUserRankActList(long userId) {
        List<Model> modelList = EntityManager.getEntityList(UserRankAct.class, userId);
        List<UserRankAct> userRankActList = new ArrayList<>();
        for (Model model : modelList) {
            userRankActList.add((UserRankAct) model);
        }
        return userRankActList;
    }
    public static UserRankAct getUserRankAct(long userId, long round, long actId) {
        Model model = EntityManager.getEntity(UserRankAct.class, userId, actId);
        if (model != null) {
            UserRankAct userRankAct = (UserRankAct) model;
            userRankAct.checkReset(round);
            return userRankAct;
        }
        return null;
    }
    public static UserCrossRankAct getUserCrossRankAct(long userId, long actId) {
        Model model = EntityManager.getEntity(UserCrossRankAct.class, userId, actId);
        if (model != null) {
            UserCrossRankAct userCrossRankAct = (UserCrossRankAct) model;
//            userCrossRankAct.checkReset(round);
            return userCrossRankAct;
        }
        return null;
    }

    public static List<UserCrossRankAct> getUserCrossRankActList(long userId) {
        List<Model> modelList = EntityManager.getEntityList(UserCrossRankAct.class, userId);
        List<UserCrossRankAct> userCrossRankActList = new ArrayList<>();
        for (Model model : modelList) {
            userCrossRankActList.add((UserCrossRankAct) model);
        }
        return userCrossRankActList;
    }

    public static UserRechargeActTask getUserRechargeActTask(long userId, int actId, int taskType) {
        String listKey = EntityManager.getFieldJoinString(userId, actId);
        Model model = EntityManager.getEntity(UserRechargeActTask.class, listKey, taskType);
        UserRechargeActTask userRechargeActTask = null;
        if (model != null) {
            userRechargeActTask = (UserRechargeActTask) model;
        }
        return userRechargeActTask;
    }
    public static List<UserRechargeActTask> getUserRechargeActTaskList(long userId, int actId) {
        String listKey = EntityManager.getFieldJoinString(userId, actId);
        List<Model> list = EntityManager.getEntityList(UserRechargeActTask.class, listKey);
        List<UserRechargeActTask> result = new ArrayList<>();
        for (Model model : list) {
            result.add((UserRechargeActTask) model);
        }
        return result;
    }

    public static UserCrossRechargeActTask getUserCrossRechargeActTask(long userId, int actId, int taskType) {
        String listKey = EntityManager.getFieldJoinString(userId, actId);
        Model model = EntityManager.getEntity(UserCrossRechargeActTask.class, listKey, taskType);
        UserCrossRechargeActTask userCrossRechargeActTask = null;
        if (model != null) {
            userCrossRechargeActTask = (UserCrossRechargeActTask) model;
        }
        return userCrossRechargeActTask;
    }
    public static List<UserCrossRechargeActTask> getUserCrossRechargeActTaskList(long userId, int actId) {
        String listKey = EntityManager.getFieldJoinString(userId, actId);
        List<Model> list = EntityManager.getEntityList(UserCrossRechargeActTask.class, listKey);
        List<UserCrossRechargeActTask> result = new ArrayList<>();
        for (Model model : list) {
            result.add((UserCrossRechargeActTask) model);
        }
        return result;
    }

    public static UserRankActShop getUserRankActShop(long userId, int actId, int shopId) {
        String listKey = EntityManager.getFieldJoinString(userId, actId);
        Model model = EntityManager.getEntity(UserRankActShop.class, listKey, shopId);
        if (model != null) {
            return (UserRankActShop) model;
        }
        return null;
    }

    public static List<UserRankActShop> getUserRankActShopList(long userId, int actId) {
        String listKey = EntityManager.getFieldJoinString(userId, actId);
        List<Model> list = EntityManager.getEntityList(UserRankActShop.class, listKey);
        List<UserRankActShop> result = new ArrayList<>();
        for (Model model : list) {
            result.add((UserRankActShop) model);
        }
        return result;
    }

    public static UserCrossRankActShop getUserCrossRankActShop(long userId, int actId, int shopId) {
        String listKey = EntityManager.getFieldJoinString(userId, actId);
        Model model = EntityManager.getEntity(UserCrossRankActShop.class, listKey, shopId);
        if (model != null) {
            return (UserCrossRankActShop) model;
        }
        return null;
    }
    public static List<UserCrossRankActShop> getUserCrossRankActShopList(long userId, int actId) {
        String listKey = EntityManager.getFieldJoinString(userId, actId);
        List<Model> list = EntityManager.getEntityList(UserCrossRankActShop.class, listKey);
        List<UserCrossRankActShop> result = new ArrayList<>();
        for (Model model : list) {
            result.add((UserCrossRankActShop) model);
        }
        return result;
    }

}
